<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of AxGridView
 *
 * @author ardha
 * @created on Oct 24, 2012
 */
Yii::import('zii.widgets.grid.CGridView');
Yii::import("ext.widgets.grid.AxLinkListPager");
Yii::import("ext.widgets.grid.AxPageSize");

class AxGridView extends CGridView {

    public $createButtonUrl = 'create';
    public $createButtonTitle = 'Create New';
    public $createButtonType = 'normal';
    public $createButtonProperty = array('htmlOptions' => array('class' => 'ax-btn', 'type' => 'button'));
    public $createTopButton = true;
    public $createBottomButton = false;
    public $deleteTopButton = true;
    public $deleteBottomButton = false;
    public $deleteButtonUrl = 'delete';
    public $enableMassAction = true;
    public $deleteConfirmation = 'Are you sure to delete the Data?';
    public $deleteButtonProperty = array('label' => 'Delete', 'cssClass' => 'ma-action', 'url' => '#', 'htmlOptions' => array('class' => 'ax-btn-delete', 'type' => 'button'));
    public $deleteUrl = 'delete';
    public $cssButtonClass = 'btn-right';
    public $enablePageSize = true;
    public $pageSize = 10;
    public $title;
    public $checkColumn = array('class' => "CCheckBoxColumn");
    public $checkBoxClmId = 'chckbx-clmn';
    private $modulesUrl = '';

    public function init() {
        $this->modulesUrl = Yii::app()->baseUrl.'/'.Yii::app()->controller->uniqueId;
        $this->deleteUrl = $this->modulesUrl.'/'.$this->deleteUrl;
        //$this->createButtonUrl = Yii::app()->createUrl($this->controller->id . '/create');
        $this->cssFile = false;
        $this->checkBoxClmId .= '-'.$this->id;
        //$this->summaryText = false;
        if ($this->deleteTopButton)
            $this->setCheckBoxColumn();
        $this->pager = array(
            //'class' => 'CLinkPager',
            'class' => 'AxLinkListPager',
            'maxButtonCount' => '8'
        );
        $this->pagerCssClass = "pagination";
        parent::init();
    }

    public function renderContent() {
        $this->renderTitleBar();
        echo '<div class="ax-body-panel">';
        parent::renderContent();
        echo '</div>';
    }

    public function renderSummary() {
        parent::renderSummary();
        if ($this->createTopButton || $this->deleteTopButton) {
            echo CHtml::openTag('div', array('class' => $this->cssButtonClass));
            $this->renderMassAction();
            if ($this->createTopButton)
                $this->renderCreateButton();
            if ($this->deleteTopButton) {
                //
                //$this->columns = array_merge(array('class' => 'CCheckBoxColumn'), $this->columns);
                echo '&nbsp;';
                $this->renderDeleteButton();
            }

            echo CHtml::closeTag('div');
            echo '<div style="clear: both;"></div>';
        }
    }

    public function renderPager() {
        /* if($this->createBottomButton || $this->deleteBottomButton){
          echo CHtml::openTag('div', array('class' => $this->cssButtonClass, 'style' => 'margin-top:2px;'));
          if ($this->createBottomButton)
          $this->renderCreateButton();
          echo CHtml::closeTag('div');
          } */
        parent::renderPager();
    }

    public function renderItems() {
        //$this->renderPageSize();
        parent::renderItems();
    }

    public function setCheckBoxColumn() {
        $this->checkColumn['htmlOptions'] = array('align' => 'center', 'valign' => 'top');
        $this->checkColumn['headerHtmlOptions'] = array('style' => 'text-align:center');
        if (!isset($this->checkColumn['selectableRows']))
            $this->checkColumn['selectableRows'] = 2;
        if (!isset($this->checkColumn["id"]))
            $this->checkColumn["id"] = $this->checkBoxClmId;


        $this->columns = array_merge(array($this->checkColumn), $this->columns);
    }

    private function renderCreateButton() {
        if ($this->createButtonType == 'long')
            $this->createButtonProperty['htmlOptions']['class'] = 'ax-btn';
        $this->createButtonProperty['htmlOptions']['onclick'] = 'MM_goToURL("' . $this->createButtonUrl . '")';
        echo CHtml::button($this->createButtonTitle, $this->createButtonProperty['htmlOptions']);
    }

    private function renderDeleteButton() {
        $this->deleteButtonProperty['htmlOptions']['id'] = 'mass-delete-act-'.$this->id;
        echo CHtml::button($this->deleteButtonProperty['label'], $this->deleteButtonProperty['htmlOptions']);
    }

    public function renderMassAction() {
        if (!$this->enableMassAction)
            return;
        $arrData = array();
        echo CHtml::openTag("div", array('style' => 'float:left;margin-right:20px;'));
        echo('<div style="float:left;padding-top:2px;margin-right:2px;">Action:</div>');
        echo CHtml::dropDownList('massaction', null, $arrData, array('id' => 'grid-mass-actions-'.$this->id, 'class' => 'mass-action-grid', 'empty' => 'Select Action'));
        echo CHtml::closeTag("div");
    }

    public function renderPageSize() {
        if (!$this->enablePageSize)
            return;

        $pageSize = array();
        $class = 'AxPageSize';
        if (is_string($this->pageSize))
            $class = $this->pageSize;
        else if (is_array($this->pageSize)) {
            $pageSize = $this->pageSize;

            if (isset($pageSize['class'])) {
                $class = $pageSize['class'];
                unset($pageSize['class']);
            }
        }
        $pageSize["afterUpdate"] = "
                $.fn.yiiGridView.update('{$this->id}');
            ";
        $pageSize['pages'] = $this->dataProvider->getPagination();
        echo CHtml::openTag("div", array("class" => "ma-actions"));
        $pageSize = $this->widget($class, $pageSize);
        echo CHtml::closeTag("div");
        //$this->renderPager();
    }

    public function renderTitleBar() {
        echo '<div class="x-panel-header-default x-horizontal x-panel-header-horizontal x-panel-header-default-horizontal x-top x-panel-header-top x-panel-header-default-top x-docked-top x-panel-header-docked-top x-panel-header-default-docked-top x-unselectable" style="height:17px;border:0px;">';
        echo '                    <span class="x-panel-header-text x-panel-header-text-default">';
        echo '                        ' . $this->title;
        echo '                    </span>';
        echo '</div>';
    }

    public function registerClientScript() {
        parent::registerClientScript();
        $cs = Yii::app()->getClientScript();
        $js = <<<EOS
$('#mass-delete-act-{$this->id}').live('click',function(){
    var arrVal = $.fn.yiiGridView.getChecked('{$this->id}','{$this->checkBoxClmId}');
    if(arrVal.length == 0){
        alert('Please select one row');
        return false;
    }
    if(!confirm('{$this->deleteConfirmation}')) return false;
    var pKey = '';
    for(i=0; i<arrVal.length; i++){
        if(i > 0)
            pKey += ',';
        pKey += arrVal[i];
    };
    var opt = {
        url:'{$this->deleteUrl}/id/'+pKey,
        success:function(data) {
            //$.fn.yiiGridView.update('{$this->id}');
            //afterDelete(th,true,data);
        },
        error:function(XHR) {
            //return afterDelete(th,false,XHR);
        }
    };
    $.fn.yiiGridView.update('{$this->id}', opt);
    return false;
});
function MM_goToURL(ulink)
{
    window.location=ulink;
}\n     
EOS;


        $cs->registerScript(__CLASS__ . '#' . $this->id, $js, CClientScript::POS_END);
    }

}

?>
